﻿using Comm.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Comm.Web.View
{
    public partial class UsersManager :Basepage
    {
        protected string strHtml = string.Empty;
        protected string strOrder = string.Empty;
        protected string strcar = string.Empty;
        protected string optHtml = string.Empty;
        BLL.UsersService bllusers = new BLL.UsersService();
        BLL.UserTypeInfoService bllusertype = new BLL.UserTypeInfoService();
        BLL.UserAuthorityService blluserauthor = new BLL.UserAuthorityService();
        StringBuilder sb = new StringBuilder(2000);
        public override void SonLoad()
        {
            DataTable dt = bllusers.GetUserList1();
            IList<UserTypeInfo> list1 = bllusertype.getWhereList("");
            IList<UserAuthority> list = blluserauthor.getAuthority();
            sb.Append("<tr><th>用户名</th><th>是否购物</th><th>是否有订单</th><th>权限设置</th><<th>操作</th></tr>");
            for (int i = 0; i < dt.Rows.Count; i++) 
            {
                optHtml = string.Empty;
                int count = 0;
                if (dt.Rows[i]["uLoginName"].ToString() != "admin")
                {
                    if (list1.Count <= 1)
                    {
                        UserTypeInfo _model = new UserTypeInfo()
                        {
                            authorityId = 3,
                            userId = int.Parse(dt.Rows[i]["uId"].ToString())
                        };
                        bllusertype.Insert(_model);
                        foreach (UserAuthority model in list)
                        {
                            if (count != 0)
                            {
                                optHtml += "<option typeid=\"" + _model.Id + "\"  value=\"" + model.Id + "\" userId=\"" + _model.userId + "\">" + model.Authority + "</option>";
                            }
                            else 
                            {
                                optHtml = "<option>请选择</option>";
                            }
                            count++;
                        }
                    }
                    else 
                    {
                        foreach (UserAuthority model in list)
                        {
                            if (count != 0)
                            {
                                if (list1[i].authorityId == model.Id)
                                {
                                    optHtml += "<option typeid=\"" + list1[i].Id + "\" userId=\"" + list1[i].userId + "\" value=\"" + model.Id + "\" selected>" + model.Authority + "</option>";
                                }
                                else 
                                {
                                    optHtml += "<option typeid=\"" + list1[i].Id + "\" userId=\"" + list1[i].userId + "\" value=\"" + model.Id + "\">" + model.Authority + "</option>";
                                }                               
                            }
                            //else
                            //{
                            //    optHtml = "<option>请选择</option>";
                            //}
                            count++;
                        }
                    }
                    if (dt.Rows[i]["Id"].ToString() != "")
                    {
                        strcar = "是";
                    }
                    else 
                    {
                        strcar = "否";
                    }
                    if (dt.Rows[i]["OrderId"].ToString() != "")
                    {
                        strOrder = "是";
                    }
                    else
                    {
                        strOrder = "否";
                    }
                    sb.Append("<tr class=\"align_Center\">");
                    sb.Append("<td style=\"width:25%; \"><span>" + dt.Rows[i]["uLoginName"].ToString() + "</span></td>");
                    sb.Append(" <td style=\"width:15%; \"><span>" + strcar + "</span></td>");
                    sb.Append("<td style=\"width:15%; \"><span class=\"price\">" + strOrder + "</span></td>");
                    sb.Append("<td style=\"width:30%; \"><select class=\"_SelectChanged\" style=\"margin: 1px;background-color: #808080;border: 1px solid #A59595;\">" + optHtml + "</select></td>");
                    sb.Append("<td style=\"width:15%; \"><a href=\"javascript:void(0)\" onclick=\"doDel("+dt.Rows[i]["uId"]+",this)\">删除</a></td></tr>");
                }
            }
            strHtml = sb.ToString();

            //<a href="javascript:void(0)" onclick="doDel(<%#Eval("bookId")%>,this)">删除</a>
        }
    }
}